<?php defined('SYSPATH') or die('No direct script access.');

 class Controller_UserInfo extends Controller{
      //
	 public function action_login()
	 {
		  $login_user=$this->request->post("login_user");

      $user_password=sha1($this->request->post("login_password"));

      if($login_user==""){

        echo json_encode(array("code"=>"001","msg"=>'请输入邮箱或者用户名'));

         exit();
      }
      if($user_password==""){

        echo json_encode(array("code"=>"001","msg"=>'请输入密码'));

        exit();

      }

      $model=new Model_User();

      echo $model->user_login($login_user,$user_password);

	 }
   public function action_register()
   {
      $user_name=$this->request->post("reg_username");

      $user_email=$this->request->post("reg_useremail");

      $user_password=$this->request->post("reg_password");

      if($user_name==""){

        echo json_encode(array("code"=>"001","error_info"=>array(

            'reg_username'=>'请输入用户名'

         )));

         exit();
      }
      if($user_email==""){

        echo json_encode(array("code"=>"001","error_info"=>array(

            'reg_email'=>'请输入邮箱'

         )));

         exit();
      }
      if($user_password==""){

        echo json_encode(array("code"=>"001","error_info"=>array(

            'reg_password'=>'请输入密码'

         )));

        exit();
      }

      $model=new Model_User();

      echo $model->register($user_name,$user_email,$user_password);

   }
   public function action_profile(){

      $firstname=$this->request->post("firstname");

      if(empty($firstname)){

        echo json_encode(array(

          'msg'=>"firstname is required"

        ));

        exit();
      }

      $lastname=$this->request->post("lastname");

      if(empty($firstname)){

        echo json_encode(array(

          'msg'=>"lastname is required"

        ));

        exit();
      }

      $new_password=$this->request->post("new_password");

      $old_password=$this->request->post("old_password");

      $user=new Model_User();

      echo $user->saveProfile($firstname, $lastname,$new_password,$old_password);

   }


   public function action_reset(){

      $user_email=$this->request->post("email");

      if(empty($user_email)){

        echo  json_encode(array(
          'msg'=>__("Please input a valid email address")
        ));
        exit();

      }

      $total= DB::select(array(DB::expr('COUNT(1)'), 'total'))->from('site_user')->where("user_email",'=',$user_email)->execute()->get("total");

      if($total==0){

        echo  json_encode(array(

          'msg'=>__("Sorry, the email is not exists")

        ));
        exit();
      }

      $token = md5(uniqid(rand())); 

      DB::insert('reset_info',array('user_email','token','createdon'))

      ->values(

        array($user_email,$token,time() 

      ))->execute();

      $url=Kohana::$config->load('site.url')."/user/reset?token=$token";

      $email_model=new Model_Email();

      $email_model->sendMail($user_email,'reset your password','You can <a href="'.$url.'" style="text-decoration:underline;color:#ff5a09">reset your password </a> now.');

      echo  json_encode(array(

        'code'=>"0x00000",

        'msg'=>'Thanks,We have send  an email to you,please check your email to reset your password.'

      ));

      exit();
   }

   public function action_resetPwd(){

      $token=$this->request->param('id');

      $query= DB::select(array(DB::expr('reset_info.user_email'), 'user_email'),'site_userid','fullname')

      ->from('reset_info')->where("reset_info.token",'=',$token)

      ->join('site_user','LEFT')->on('site_user.user_email' , '=' , 'reset_info.user_email' )

      ->execute();

      $user_email=$query->get("user_email");

      if(empty($user_email)){

        echo  json_encode(array(

          'msg'=>"Sorry,the token has expired!"

        ));

        exit();
      }

      $new_password=sha1($this->request->post("password"));

      $sql="update site_user

            set user_pwd='$new_password'

            where user_email='$user_email'";  

      $total_rows=DB::query(null,$sql)->execute();

      $sql="delete from reset_info

            where  token='$token'";

      DB::query(null,$sql)->execute();

      if($total_rows>0){

         echo  json_encode(array(

          'code'=>"0x00000",

          'msg'=>"Thanks,Change the password successfully."

          ));

        exit();
      }
      else{
        
         echo  json_encode(array(

          'code'=>"0x00000",

          'msg'=>"Sorry,try it again!"

          ));

        exit();
      }
   }
 } 
